package com.gitee.wsl.net.ssdp.description.model.xml


import com.gitee.wsl.logger.Logger
import com.gitee.wsl.logger.info
import com.gitee.wsl.net.ssdp.description.xml.XmlElement
import kotlin.properties.Delegates

data class SystemVersion(
    val hardware: Int,
    val major: Int,
    val minor: Int,
    val patch: Int,
    val buildNumber: Int,
    val display: String
){
    companion object{
        /**
         * Facilitates fast and flexible deserialization and conditional validation.
         */
        fun fromXml(xml: XmlElement): SystemVersion {
            var hardwareI by Delegates.notNull<Int>()
            var majorI by Delegates.notNull<Int>()
            var minorI by Delegates.notNull<Int>()
            var patchI by Delegates.notNull<Int>()
            var buildNumberI by Delegates.notNull<Int>()
            var displayS by Delegates.notNull<String>()

            xml.children?.forEach {
                when(it.tag){
                    "HW" -> hardwareI = it.get()
                    "Major" -> majorI = it.get()
                    "Minor" -> minorI = it.get()
                    "Patch" -> patchI = it.get()
                    "Buildnumber" -> buildNumberI = it.get()
                    "Display" -> displayS = it.get()
                    else -> Logger.info("Unexpected tag ${it.tag}")
                }
            }
            return SystemVersion(hardwareI, majorI, minorI, patchI, buildNumberI, displayS)
        }
    }
}